package fpsf
{
	import mx.events.BrowserChangeEvent;
	import mx.managers.BrowserManager;
	import mx.managers.IBrowserManager;
	
	public class History
	{
		private var bm:IBrowserManager;
		private var sitemap:Sitemap; //(index op i = 0)
		
		public function History(sitemap:Sitemap)
		{
			this.sitemap = sitemap;
			bm = BrowserManager.getInstance(); 
			bm.addEventListener(BrowserChangeEvent.URL_CHANGE, parseURL);
			bm.init(null, sitemap.getTitle(0);
			if (Application.application.parameters.view != null && Application.application.parameters.view != sitemap.getFragment(0) {	
				bm.setFragment(Application.application.parameters.view);
			}
		}

		private function parseURL(event:Event):void
		{
			for (var i:int = 0; i < sitemap.asArray().length; i++) {
				if (sitemap.getFragment(i) == bm.fragment) {
					if (currentState != sitemap.getState(i)) {
						currentState = sitemap.getState(i);
						bm.setTitle(sitemap.getTitle(i));
					}
					return;
				}
			}
		}
		
	}
}